package com.ruoyi.project.user.couple.service.impl;


import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.user.couple.domain.Couple;
import com.ruoyi.project.user.couple.mapper.CoupleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 新人service业务成管理
 */
@Service
public class CoupleServiceImpl implements CoupleService {
    @Autowired
    private CoupleMapper coupleMapper;
    @Autowired
    private IUserService iUserService;

    @Value ( "${my.mysql.post.hostid}" )
    private Long coupleId;
    /**
     * 查询新人id
     * @param coupleId
     * @return
     */
    @Override
    public Couple selectCoupleById(Long coupleId) {
        return coupleMapper.selectCoupleById (coupleId  );
    }

    /**
     * 查询新人列表
     * @param couple
     * @return
     */
    @Override
    public List <Couple> selectCoupleList(Couple couple) {
        return coupleMapper.selectCoupleList ( couple );
    }

    /**
     * 添加新人
     * @param couple
     * @return
     */
    @Override
    public int insertCouple(Couple couple) {
        Date date = new Date();
        User user =new User (  );
        user.setLoginName ( couple.getCoupleGroomname () );
        user.setPhonenumber ( couple.getCouplePhone () );
        user.setEmail ( couple.getCoupleEmail () );
        user.setUserName ( couple.getCouplePhone () );
        user.setCreateTime ( date );
        Long [] postIds ={coupleId};
        user.setPostIds ( postIds );
        int index = iUserService.insertUser ( user );
        if(index>0){
            couple.setCoupleUserId ( user.getUserId () );
            couple.setCreateTime ( date );
            int index2= coupleMapper.insertCouple ( couple );
            if (index2>0){
                return 1;
            }
        }
        return 0;
    }

    /**
     * 修改新人
     * @param couple
     * @return
     */
    @Override
    public int updateCouple(Couple couple) {
        return coupleMapper.updateCouple ( couple );
    }

    /**
     * 删除新人对象
     * @param ids
     * @return
     */
    @Override
    public int deleteCoupleByIds(String ids) {
        return coupleMapper.deleteCoupleByIds ( Convert.toStrArray (ids));
    }

    @Override
    public int updateState(Couple couple) {
        Couple selectCouple = coupleMapper.selectCoupleById ( (long) couple.getCoupleId () );
        selectCouple.getCoupleUserId ();
        User user =new User ();
        user.setUserId (selectCouple.getCoupleUserId () );
        user.setStatus (couple.getCoupleState()==2?"1":"0" );
        user.setUpdateBy (ShiroUtils.getLoginName() );
        iUserService.updateUserInfo ( user );
        return coupleMapper.updateCoupleById ( couple );
    }
}
